import os
import sys
import numpy as np

from PIL import Image
from pathlib import Path


if __name__ == "__main__":


    STEP_AUDIO_TO_EXPRESSION = True
    STEP_CHOOSE_BG = False

    STEP_RENDER = True
    STEP_BLEND_BG = False
    STEP_GAN_VIDEO = False

    #########################################
    #  base path
    path_data = Path("../data")
    path_checkpoints = Path("../checkpoints")
    path_expr = Path("../experiments")
    path_save = Path("../data/tmp")

    # audio
    path_audio = path_data.joinpath('audio/input/03Fsi1201.wav')

    #path_audio = path_data.joinpath('audio/input/5_00006.wav')
    path_audio_save = path_data.joinpath('tmp/audio')

    # video
    # path_video = path_data.joinpath('tmp/video/31-25fps-360p.mp4')
    path_video = path_data.joinpath('tmp/video/31.mp4')
    path_video_save = path_data.joinpath('tmp/video')

    audio_name = path_audio.stem
    video_name = path_video.stem


    ###########################################
    # STEP 1： Audio to  3DMM Coeff
    if STEP_AUDIO_TO_EXPRESSION:
        cmd = f"cd audio; \
                python inference.py --path_input {path_audio} --path_save {path_audio_save}"

        print(f"[xxxxx] RUN: \n[xxxxx] {cmd}")
        os.system(cmd)



    ###########################################
    # STEP 2： Background matching
    if STEP_CHOOSE_BG:
        pass


    ###########################################
    # STEP 3： Render to 3DMM
    if STEP_RENDER:
        cmd = f"cd reconstruction; \
                CUDA_VISIBLE_DEVICES=0; \
                python render.py --path_video {path_video} --path_audio {path_audio} --path_save {path_save}"

        print(f"[xxxxx] RUN: \n[xxxxx] {cmd}")
        os.system(cmd)

    ###########################################
    # STEP 4: Blend rendered image with background
    if STEP_BLEND_BG:
        pass



    ###########################################
    # STEP 5: Render to video
    if STEP_GAN_VIDEO:
        pass